import * as THREE from 'three';
import vertexShader from '@/shader/point/vertexShader.glsl';
import fragmentShader from '@/shader/point/fragmentShader.glsl';

const textureLoader = new THREE.TextureLoader();

export default function (color) {
  const group = new THREE.Group();
  const config = {
    color: '#6cb6fa',
    opacity: 1,
    maxSize: 45,
    minSize: 5,
    number: 200,
    encircleInRadius: 1400,
    encircleOutRadius: 4000,
    rotateSpeed: 50
  };
  const material = new THREE.ShaderMaterial({
    uniforms: {
      size: { value: 30 },
      scale: { value: 500 },
      radius: { value: 1000 },
      opacity: { value: 0.5 },
      color: { value: new THREE.Color(color) },
      pointTexture: { value: textureLoader.load('images/dot.png') }
    },
    vertexShader,
    fragmentShader,
    transparent: true
  });

  var o = Math.floor(config.number / 2);
  var s = config.number - o;
  var l = new Float32Array(s);
  var u = new Float32Array(3 * s);
  var c = new Float32Array(o);
  var h = new Float32Array(3 * o);
  for (var p = 0; p < s; p++) {
    var d = getRandomPosition(config.encircleInRadius, config.encircleOutRadius);
    u[3 * p + 0] = d.x;
    u[3 * p + 1] = d.y;
    u[3 * p + 2] = d.z;
    l[p] = config.minSize + (config.maxSize - config.minSize) * Math.random();
  }
  var m = new THREE.BufferGeometry();
  m.setAttribute('position', new THREE.BufferAttribute(u, 3));
  m.setAttribute('size', new THREE.BufferAttribute(l, 1));
  group.add(new THREE.Points(m, material));
  for (var p1 = 0; p1 < o; p++) {
    var f = getRandomPosition(config.encircleInRadius, config.encircleOutRadius);
    h[3 * p1 + 0] = f.x;
    h[3 * p1 + 1] = f.y;
    h[3 * p1 + 2] = f.z;
    c[p1] = config.minSize + (config.maxSize - config.minSize) * Math.random();
  }
  var g = new THREE.BufferGeometry();
  g.setAttribute('position', new THREE.BufferAttribute(h, 3));
  g.setAttribute('size', new THREE.BufferAttribute(c, 1));
  group.add(new THREE.Points(g, material));
  return {
    mesh: group,
    update() {
      // var e = 50;
      // var particlesLeft = group.children[1];
      // var particlesRight = group.children[0];
      // // particlesLeft.rotation.y -= 0.00002 * e;
      // // particlesRight.rotation.y += 0.00002 * e;
      // particlesLeft.rotation.y += 0.005;
      // particlesRight.rotation.y -= 0.005;
    },
    setColor(color) {
      if (!material) return;
      material.uniforms.color.value = new THREE.Color(color);
    }
  };
}
function getRandomPosition(e, t) {
  var a = new THREE.Vector3(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5);
  a.normalize();
  a.multiplyScalar(e + (t - e) * Math.random());
  return a;
}
